/**
 * Created by WT-SUN on 2017/3/15.
 */
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['600px', '60%'],
        content: content
    });
}
/**
 * 打开新建菜单框
 */
function addResources() {
    var pageParentId = $('#parentId').val();
    var title = '新增菜单';
    var content = ROOT_PATH + '/permission/resource/toAddResourcesPage.shtml?pageParentId=' + pageParentId;
    openLayers(title, content);
}
function getTreeNodeText() {
    var selectedTreeNode=$("#resourceTree").tree('getSelected');
    var treeName="0";
    if (selectedTreeNode!=null){
        treeName=selectedTreeNode.text;
    }
    return treeName;
}
/**
 * 编辑菜单
 */
function editResources() {
    var rows = $('#treetb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的行！");
        return
    }
    var id = rows[0].id;
    var title = '编辑菜单';
    var content = ROOT_PATH + '/permission/resource/toEditResourcesPage.shtml?id=' + id;
    openLayers(title, content);
}
/**
 * 重新加载表格和树
 */
function reLoadData() {
    reLoadTreeTb();
    reLoadTree();
}
/**
 * 重新加载表格
 */
function reLoadTreeTb() {
    /*var queryParams = $('#treetb').datagrid('options').queryParams;
     queryParams.parentId= $('#parentId').val();*/
    $('#treetb').datagrid('clearSelections');
    var queryParams = $('#queryForm').serializeJson();
    queryParams["parentId"] = $('#parentId').val();
    queryParams["sort"] = 'sort';
    queryParams["order"] = 'asc';
    $('#treetb').datagrid('options').queryParams = queryParams;
    $("#treetb").datagrid('reload', ROOT_PATH + '/permission/resource/treeTable.shtml');
}
/**
 * 重新加载树
 */
function reLoadTree() {
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/resource/selectByPrimaryKey.shtml',
        data: {id: ''},
        dataType: "json",
        success: function (data) {
            $('#resourceTree').tree({
                data: data
            });
            var node = $('#resourceTree').tree('find', $('#parentId').val());
            $('#resourceTree').tree('select', node.target);
            $('#resourceTree').tree('expand', node.target);
            $('#resourceTree').tree('expandTo', node.target);
        }
    });
}
/**
 * 删除菜单
 */
function deleteResources() {
    var rows = $('#treetb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return;
    }
    var id = rows[0].id;
    var node = $('#resourceTree').tree('find', id);
    var objData = $('#resourceTree').tree('getData', node.target);
    if (objData.children.length > 0) {
        layer.alert("请先对子菜单进行删除！");
        return;
    }
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/permission/resource/deleteByPrimaryKey.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#treetb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}

$(function () {
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/resource/selectByPrimaryKey.shtml',
        data: {id: ''},
        dataType: "json",
        success: function (data) {
            $('#parentId').val('0');
            $('#resourceTree').tree({
                data: data,
                onClick: function (node) {
                    $('#parentId').val(node.id);
                    $('#resourceTree').tree('expand', node.target);
                    $('#treetb').datagrid('clearSelections');
                    reLoadTreeTb();
                }
            });
        }
    });
    $('#treetb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/permission/resource/treeTable.shtml?parentId=0',
        queryParams:{
            sort:'sort',
            order:'asc'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'name', title: '菜单名称', width: 100, sortable: true},
            {field: 'code', title: '菜单标识', width: 100},
            {field: 'resUrl', title: '菜单地址', width: 100},
            {
                field: 'resType', title: '菜单类型', width: 70, sortable: true, formatter: function (value, row, index) {
                return row.resType == '0' ? '<span class="color-primary text-white" style="padding: 3px;border-radius:5px;">目录</span>' : row.resType =='1'?'<span class="color-info text-white" style="padding: 3px;border-radius:5px;">菜单</span>':'<span class="color-warning text-white" style="padding: 3px;border-radius:5px;">按钮</span>';
            }
            },
            {field: 'btnStyle', title: '按钮样式', width: 100, formatter:function (value, row, index) {
                return '<i class="'+ value +'"></i>'
            }},
            {
                field: 'useState', title: '启用状态', width: 100, sortable: true, formatter: function (value, row, index) {
                return row.useState == '1' ? '<span class="color-success text-white" style="padding: 3px;border-radius:5px;">启用</span>' : '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">未启用</span>';
            }
            },
            {field: 'sort', title: '排序数字', width: 100, sortable: true},
            {field: 'remarks', title: '备注', width: 100},
            {field: 'addTime', title: '添加时间', width: 100, sortable: true},
            {field: 'updateTime', title: '修改时间', width: 100, sortable: true}
        ]]
    });

    $('#btn_resource_add').on('click',function () {
        addResources();
    });
    $('#btn_resource_edit').on('click',function () {
        editResources();
    });
    $('#btn_resource_delete').on('click',function () {
        deleteResources();
    });
});
